<?php

namespace App\Http\Controllers\Common;

use App\Package\Code\Code;
use Illuminate\Http\Request;
use App\Package\Message\Message;
use App\Http\Controllers\Controller;

/**
 * 通知短息逻辑控制器
 *
 * Date: 2018/06/01
 * @author Tenny
 * @package App\Http\Controllers\Common
 */
class MessageController extends Controller
{
    /**
     * 发送验证码
     *
     * Date: 2018/06/01
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function sendCode(Request $request)
    {
        $this->validate($request, [
            'phone' => ['required', 'regex:/^(1[3456789])\d{9}$/']
        ], [
            'phone.required' => '请填写您的手机号码',
            'phone.regex' => '手机号码格式错误'
        ]);

        $mobile = $request->get('phone');
        $message = new Message();
        $code = Code::generator();
        $response = $message->code($mobile, $code);

        if ($response->code == 2) {
            return message('验证码已发送');
        }
        return failed((string)$response->msg);
    }
}
